<?php
namespace App\Api\V1\Models;

use Illuminate\Database\Eloquent\Model;

class UserAddress extends Model
{
    protected $table = 'user_address';
    public $timestamps = false;
    protected $primaryKey = 'user_id';


    /**
     * 获取指定账号的收货地址列表
     * @access public
     * @param array $data 外部数据
     * @return array|false
     * @throws
     */
    public function getAddressList(array $data)
    {
        // 实际查询
        $res = user_address::select()->limit('10');

        return json_encode($res);
    }

    /**
     * 获取指定账号的一个收货地址
     * @access public
     * @param array $data 外部数据
     * @return array|false
     * @throws
     */
    public function getAddressItem(array $data)
    {

        $res = user_address::where('user_address_id',5)->first()->toArray();
        if ($res){
            return $res;
        }

    }



    /**
     * 获取指定账号的默认收货地址信息
     * @access public
     * @param array $data 外部数据
     * @return array|false
     * @throws
     */
    public function getAddressDefault(array $data)
    {

        $res = user_address::first(1)->toArray();
        if ($res){
            return $res;
        }

    }



    public function addAddressItem(array $data)
    {
        $data = [
            "consignee" => "周晓",
            "country" => '0',
            "region_list" => '[1,2,3]',
            "address" => '江北区',
            "zipcode" => '315000',
            "tel" => '0574-87487585',
            "mobile" => '13071297155',
            "is_default" => '1'
        ];
        $this->consignee = $data['consignee'];
        $this->country = $data['country'];
        $this->region_list = $data['region_list'];
        $this->address = $data['address'];
        $this->zipcode = $data['zipcode'];
        $this->tel = $data['tel'];
        $this->mobile = $data['mobile'];
        $this->is_default = $data['is_default'];
        if ($this->save()) {
            return $this->toArray();
        }

        return false;
    }


    /**
     * 编辑一个收获地址
     * @access public
     * @param array $data 外部数据
     * @return array|false
     */
    public function setAddressItem(array $data)
    {
        $ds = [
            "user_address_id" => "3",
            "consignee" => "李明",
            "country" => '0',
            "region_list" => '[1,2,3]',
            "address" => '海曙区',
            "zipcode" => '315000',
            "tel" => '0574-87487585',
            "mobile" => '13071297155',
            "is_default" => '1'
        ];
        $res = user_address::where('user_address_id',3)->update($ds);
        if ($res){
            return true;
        }
        return false;
    }

    /**
     * 批量删除收货地址
     * @access public
     * @param array $data 外部数据
     * @return array|false
     * @throws
     */
    public function delAddressList(array $data)
    {
        $user_address_id = [21,22,23,24,25,26];
        $res = user_address::where('user_address_id','$user_address_id')->delete();
        if ($res){
            return $res;
        }

    }

}
